package com.lzxzjy.serivce.impl;

import com.lzxzjy.mapper.AnnouncementMapper;
import com.lzxzjy.pojo.Announcement;
import com.lzxzjy.pojo.PageBean;
import com.lzxzjy.serivce.AnnouncementService;
import com.lzxzjy.util.SqlSessionFactoryUtils;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;

import java.util.List;

public class AnnouncementServiceImpl implements AnnouncementService {
    private final SqlSessionFactory factory = SqlSessionFactoryUtils.getSqlSessionFactory();

    @Override
    public boolean insertAnnouncement(Announcement announcement) {
        SqlSession announcementSqlSession = factory.openSession();
        AnnouncementMapper announcementMapper = announcementSqlSession.getMapper(AnnouncementMapper.class);
        Integer integer = announcementMapper.insertAnnouncement(announcement);
        announcementSqlSession.commit();
        announcementSqlSession.close();
        return integer != 0;
    }

    @Override
    public PageBean<Announcement> selectAnnouncementByPageAndTerms(Announcement announcement, int currentPage, int pageSize) {
        SqlSession announcementSqlSession = factory.openSession();
        AnnouncementMapper announcementMapper = announcementSqlSession.getMapper(AnnouncementMapper.class);
        int begin = (currentPage - 1) * pageSize;
        List<Announcement> announcements = announcementMapper.selectAnnouncementByPageAndTerms(announcement, begin, pageSize);
        Integer announcementTotalCountByTerms = announcementMapper.selectAnnouncementTotalCountByTerms(announcement);
        PageBean<Announcement> announcementPageBean = new PageBean<>();
        announcementPageBean.setRows(announcements);
        announcementPageBean.setTotalCount(announcementTotalCountByTerms);
        announcementSqlSession.close();
        return announcementPageBean;
    }

    @Override
    public boolean deleteAnnouncementByIds(int[] announcementIds) {
        SqlSession announcementSqlSession = factory.openSession();
        AnnouncementMapper announcementMapper = announcementSqlSession.getMapper(AnnouncementMapper.class);
        Integer integer = announcementMapper.deleteByAnnouncementIds(announcementIds);
        announcementSqlSession.commit();
        announcementSqlSession.close();
        return integer != 0;
    }

    @Override
    public boolean deleteAnnouncementById(int announcementId) {
        SqlSession announcementSqlSession = factory.openSession();
        AnnouncementMapper announcementMapper = announcementSqlSession.getMapper(AnnouncementMapper.class);
        Integer integer = announcementMapper.deleteByAnnouncementId(announcementId);
        announcementSqlSession.commit();
        announcementSqlSession.close();
        return integer != 0;
    }

    @Override
    public boolean updateAnnouncementById(Announcement announcement) {
        SqlSession announcementSqlSession = factory.openSession();
        AnnouncementMapper announcementMapper = announcementSqlSession.getMapper(AnnouncementMapper.class);
        Integer integer = announcementMapper.updateByAnnouncementId(announcement);
        announcementSqlSession.commit();
        announcementSqlSession.close();
        return integer != 0;
    }

    @Override
    public List<Announcement> selectAllAnnouncement() {
        SqlSession announcementSqlSession = factory.openSession();
        AnnouncementMapper announcementMapper = announcementSqlSession.getMapper(AnnouncementMapper.class);
        return announcementMapper.selectAllAnnouncement();
    }
}
